from django.core.cache import cache
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View

from apps.address.models import Area


class AreasView(View):
    def get(self, request):
        # 先判断缓存有没有
        # areas_dict  = cache.get('areas_dict')
        # if not areas_dict:
        try:
            areas = Area.objects.all()
        except:
            return JsonResponse({'errno':4001, 'errmsg':'DBERR'})
        areas_dict = []
        for area in areas:
            areas_dict.append({
                'aid':area.id,
                'aname':area.name
            })
            # cache.set('areas_dict', areas_dict, 3)
        return JsonResponse({'errno':0, 'errmsg':'ok', 'data':areas_dict})
